﻿// -----------------------------------------------------------------------
// <copyright file="CirclesDictionary.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex3
{
    using System;
    using System.IO;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using FacebookWrapper;
    using FacebookWrapper.ObjectModel;
    using Ex3.Logic;
    using Ex3.Repository;
    
    /// <summary>
    /// Represents the circles of the user
    /// </summary>
    public class CirclesOfFriendsClient
    {
        public List<Circle> m_Circles { get; set; }

        public string TypeCircleShown { get; set; }

        public CirclesOfFriendsClient()
        {
            m_Circles  = new List<Circle>();
        }

        public void addNewCircle(Circle i_Circle)
        {
            if (i_Circle != null)
            {
                m_Circles.Add(i_Circle);  
            }
        }

        private void ChengeTheListAfterDelete(List<Circle> circlesList)
        {
            m_Circles = circlesList;
        }
        
        public void Save(User i_LoggedinUser)
        {
            RepositoryFactory repository = FactoryMaker.GetFactory(i_LoggedinUser);
            repository.CreateCircleRepository().SaveCircles(this.m_Circles);
        }

        public void Load(User i_LoggedinUser)
        {
            List<Circle> circlesList;
            RepositoryFactory repository = FactoryMaker.GetFactory(i_LoggedinUser);
            circlesList =
                (List<Circle>)repository.CreateCircleRepository().LoadCircles();
                   
            if (circlesList != null)
            {
                m_Circles = circlesList;
            }
        }

        public string ChooseRandomProfilePhoto(Circle i_Circle)
        {
            string randomPrifilePhotos = string.Empty;

            if (i_Circle.FriendsInCircle.Count > 0)
            {
                List<string> keys = new List<string>();

                foreach (SerializableUserInCircuit friend in i_Circle.FriendsInCircle)
                {
                    keys.Add(friend.URLPhoto);
                }

                Random random = new Random();
                int randomKey = random.Next(0, keys.Count);

                randomPrifilePhotos = keys[randomKey];
            }

            return randomPrifilePhotos;
        }

        ////delete a friend from circle by giving the name and the circle
        public void DeleteFriendFromCircuit(string i_UserName, Circle i_CircleToDeleteAFriend)
        {
            foreach (SerializableUserInCircuit findSreilizedUser in i_CircleToDeleteAFriend.FriendsInCircle)
            {
                if (findSreilizedUser.Name == i_UserName)
                {
                    i_CircleToDeleteAFriend.FriendsInCircle.Remove(findSreilizedUser);
                    return;
                }
            }
        }

        public void DeleteCircle(List<Circle> i_CircleList, Circle i_CircleToDelete)
        {
            i_CircleList.Remove(i_CircleToDelete);
        }

        public void AddFriendToCircle(User i_Friend, Circle i_Circle)
        {
            SerializableUserInCircuit serializableFriend = new SerializableUserInCircuit(i_Friend);
            i_Circle.FriendsInCircle.Add(serializableFriend);
        }

        public Circle GetCircleByType(string i_typeCircle)
        {
            Circle tempCircle = null;

            foreach (Circle circle in m_Circles)
            {
                if (circle.Name == i_typeCircle)
                {
                    tempCircle = circle;
                }
            }

            return tempCircle;
        }
    }
}
